using System;
using System.Collections.Generic;
using System.Text;
using Csla;
using Csla.Data;
using Active.Core.DB;

namespace Active.Core
{
    [Serializable()]
    public class Region : BusinessBase<Region>
    {
        #region Business Methods

        // TODO: add your own fields, properties and methods
        private int _id;

        public int Id
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _id;
            }
        }

        string _description = string.Empty;
        public string Description
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _description;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_description != value)
                {
                    _description = value;
                    PropertyHasChanged();
                }
            }
        }

        string _name = string.Empty;
        public string Name
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return _name;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (_name != value)
                {
                    _name = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        internal static Region NewRegion()
        {
            // TODO: change to use new keyword if not loading defaults
            return new Region();
            //return DataPortal.Create<Region>();
        }

        internal static Region GetRegion(SafeDataReader dr)
        {
            return new Region(dr);
        }

        private Region()
        {
            MarkAsChild();
        }

        private Region(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            _description = dr.GetString("Description");
            _id = dr.GetInt32("Id");
            _name = dr.GetString("Name");

            MarkOld();
        }

        internal void Insert()
        {
            NBearLite.InsertSqlSection insert =
                DataService.DBService.Insert(Tables.Regions)
                .AddColumn(Tables.Regions.Description, _description)
                .AddColumn(Tables.Regions.Name, _name);
            _id = insert.ExecuteReturnAutoIncrementID(Tables.Regions.Id);
            MarkOld();
        }

        internal void Update()
        {
            NBearLite.UpdateSqlSection update =
                DataService.DBService.Update(Tables.Regions)
                .AddColumn(Tables.Regions.Description, _description)
                .AddColumn(Tables.Regions.Name, _name)
                .Where(Tables.Regions.Id == _id);
            update.Execute();

            MarkOld();
        }

        internal void DeleteSelf()
        {
            DataService.DBService.Delete(Tables.Regions)
                .Where(Tables.Regions.Id == _id).Execute();

            MarkNew();
        }

        #endregion
    }
}
